﻿	var textPadding = 3; // Padding at the left of tab text - bigger value gives you wider tabs
	var strictDocType = true;
	var tabView_maxNumberOfTabs = 6;	// Maximum number of tabs

	/* Don't change anything below here */
	var dhtmlgoodies_tabObj = {};//new Array()
	var activeTabIndex = new Array();
	var MSIE = navigator.userAgent.indexOf('MSIE')>=0?true:false;

	var regExp = new RegExp(".*MSIE ([0-9]\.[0-9]).*","g");
	var navigatorVersion = navigator.userAgent.replace(regExp,'$1');

	var ajaxObjects = new Array();
	var tabView_countTabs = new Array();
	var tabViewHeight = new Array();
	var tabDivCounter = 0;
	var closeImageHeight = 8;	// Pixel height of close buttons
	var closeImageWidth = 8;	// Pixel height of close buttons
	var imgPath = getRelativePath() + "/lovejes/tab/images/";

	function setImgPath(path){
		imgPath = path;
	}

	/* 添加用户的方法 在点击tab时执行 add by Jacky Chen 2008-7-8*/
	var userFunctionOnTabClick = function(){};
	function addUserFunctionOnTabClick(id,func){
		userFunctionOnTabClick[id] = func;
	}
	//parentId,tabId "tabView_level_1","0"
	function addUserFunctionOnTabClick(parentId,tabId,func){
		userFunctionOnTabClick["tabTab"+ parentId+"_"+tabId] = func;
	}
	function setPadding(obj,padding){
		var span = obj.getElementsByTagName('SPAN')[0];
		span.style.paddingLeft = padding + 'px';
		span.style.paddingRight = padding + 'px';
	}
	function showTab(parentId,tabIndex)
	{
		var parentId_div = parentId + "_";
		if(!document.getElementById('tabView' + parentId_div + tabIndex)){
			return;
		}
		if(activeTabIndex[parentId]>=0){
			if(activeTabIndex[parentId]==tabIndex){
				return;
			}

			var obj = document.getElementById('tabTab'+parentId_div + activeTabIndex[parentId]);

			obj.className='tabInactive';
			var img = obj.getElementsByTagName('IMG')[0];
			if(img.src.indexOf('tab_')==-1)img = obj.getElementsByTagName('IMG')[1];
			img.src = imgPath+'tab_right_inactive.gif';
			document.getElementById('tabView' + parentId_div + activeTabIndex[parentId]).style.display='none';
		}

		var thisObj = document.getElementById('tabTab'+ parentId_div +tabIndex);

		thisObj.className='tabActive';
		var img = thisObj.getElementsByTagName('IMG')[0];
		if(img.src.indexOf('tab_')==-1)img = thisObj.getElementsByTagName('IMG')[1];
		img.src = imgPath + 'tab_right_active.gif';

		document.getElementById('tabView' + parentId_div + tabIndex).style.display='block';
		activeTabIndex[parentId] = tabIndex;


		var parentObj = thisObj.parentNode;
		var aTab = parentObj.getElementsByTagName('DIV')[0];
		countObjects = 0;
		var startPos = 2;
		var previousObjectActive = false;
		while(aTab){
			if(aTab.tagName=='DIV'){
				if(previousObjectActive){
					previousObjectActive = false;
					startPos-=2;
				}
				if(aTab==thisObj){
					startPos-=2;
					previousObjectActive=true;
					setPadding(aTab,textPadding+1);
				}else{
					setPadding(aTab,textPadding);
				}

				aTab.style.left = startPos + 'px';
				countObjects++;
				startPos+=2;
			}
			aTab = aTab.nextSibling;
		}

		return;
	}

	function tabClick()
	{
		var idArray = this.id.split('_');

		showTab(this.parentNode.parentNode.id,idArray[idArray.length-1].replace(/[^0-9]/gi,''));
		/* 添加用户的方法 在点击tab时执行 add by Jacky Chen 2008-7-8*/
		if(userFunctionOnTabClick && userFunctionOnTabClick[this.id] && userFunctionOnTabClick[this.id] instanceof Function){
			userFunctionOnTabClick[this.id](this);
		}
	}

	function rolloverTab()
	{
		if(this.className.indexOf('tabInactive')>=0){
			this.className='inactiveTabOver';
			var img = this.getElementsByTagName('IMG')[0];
			if(img.src.indexOf('tab_')<=0)img = this.getElementsByTagName('IMG')[1];
			img.src = imgPath + 'tab_right_over.gif';
		}

	}
	function rolloutTab()
	{
		if(this.className ==  'inactiveTabOver'){
			this.className='tabInactive';
			var img = this.getElementsByTagName('IMG')[0];
			if(img.src.indexOf('tab_')<=0)img = this.getElementsByTagName('IMG')[1];
			img.src = imgPath + 'tab_right_inactive.gif';
		}

	}

	function hoverTabViewCloseButton()
	{
		this.src = this.src.replace('close.gif','close_over.gif');
	}

	function stopHoverTabViewCloseButton(){
		this.src = this.src.replace('close_over.gif','close.gif');
	}
	
	function setTab(no,title){
		Jes.html('s_tabTabtabView_1_'+no,title);
	}
	
	//var dd=1;
	function initTabs(mainContainerID,tabTitles,activeTab,width,height,closeButtonArray,additionalTab){
		if(!closeButtonArray)closeButtonArray = new Array();

		if(!additionalTab || additionalTab=='undefined'){
			dhtmlgoodies_tabObj[mainContainerID] = document.getElementById(mainContainerID);
			width = width + '';
			if(width.indexOf('%')<0)width= width + '';
			dhtmlgoodies_tabObj[mainContainerID].style.width = width;

			height = height + '';
			if(height.length>0){
				if(height.indexOf('%')<0)height= height + '';
				//dhtmlgoodies_tabObj[mainContainerID].style.height = height;
			}


			tabViewHeight[mainContainerID] = height;

			var tabDiv = document.createElement('DIV');
			var firstDiv = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[0];

			dhtmlgoodies_tabObj[mainContainerID].insertBefore(tabDiv,firstDiv);
			tabDiv.className = 'dhtmlgoodies_tabPane';
			tabView_countTabs[mainContainerID] = 0;

		}else{
			var tabDiv = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[0];
			var firstDiv = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV')[1];
			height = tabViewHeight[mainContainerID];
			activeTab = tabView_countTabs[mainContainerID];
		}

		for(var no=0;no<tabTitles.length;no++){
			var aTab = document.createElement('DIV');
			aTab.id = 'tabTab' + mainContainerID + '_' +  (no + tabView_countTabs[mainContainerID]);
			aTab.onmouseover = rolloverTab;
			aTab.onmouseout = rolloutTab;
			aTab.onclick = tabClick;
			aTab.className='tabInactive';
			tabDiv.appendChild(aTab);
			var span = document.createElement('SPAN');
			span.id='s_'+aTab.id;
			span.innerHTML = tabTitles[no];
			span.style.position = 'relative';
			aTab.appendChild(span);

			if(closeButtonArray[no]){
				var closeButton = document.createElement('IMG');
				closeButton.src = imgPath + 'close.gif';
				closeButton.height = closeImageHeight + 'px';
				closeButton.width = closeImageHeight + 'px';
				closeButton.setAttribute('height',closeImageHeight);
				closeButton.setAttribute('width',closeImageHeight);
				closeButton.style.position='absolute';
				closeButton.style.top = '6px';
				closeButton.style.right = '0px';
				closeButton.onmouseover = hoverTabViewCloseButton;
				closeButton.onmouseout = stopHoverTabViewCloseButton;

				span.innerHTML = span.innerHTML + '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';

				var deleteTxt = span.innerHTML+'';

				closeButton.onclick = function(){delTab(this.parentNode.innerHTML) };
				span.appendChild(closeButton);
			}

			var img = document.createElement('IMG');
			img.valign = 'bottom';
			img.src = imgPath + 'tab_right_inactive.gif';
			// IE5.X FIX
			if((navigatorVersion && navigatorVersion<6) || (MSIE && !strictDocType)){
				img.style.styleFloat = 'none';
				img.style.position = 'relative';
				img.style.top = '4px'
				span.style.paddingTop = '4px';
				aTab.style.cursor = 'hand';
			}	// End IE5.x FIX
			aTab.appendChild(img);
		}

		var tabs = dhtmlgoodies_tabObj[mainContainerID].getElementsByTagName('DIV');
		var divCounter = 0;
		for(var no=0;no<tabs.length;no++){
			if(tabs[no].className=='dhtmlgoodies_aTab' && tabs[no].parentNode.id == mainContainerID){
				if(height.length>0){
					tabs[no].style.minHeight=height;
					tabs[no].style.height = height;
				}
				tabs[no].style.display='none';
				tabs[no].id = 'tabView' + mainContainerID + "_" + divCounter;
				divCounter++;
			}
		}
		tabView_countTabs[mainContainerID] = tabView_countTabs[mainContainerID] + tabTitles.length;
		showTab(mainContainerID,activeTab);

		return activeTab;
	}

	function showAjaxTabContent(ajaxIndex,parentId,tabId,func){
		var obj = document.getElementById('tabView'+parentId + '_' + tabId);
		obj.innerHTML = ajaxObjects[ajaxIndex].response;
		if(func && func instanceof Function){//hyh增加的方法 2008-7-24 15:52
			func();
		}
	}

	function resetTabIds(parentId){
		var tabTitleCounter = 0;
		var tabContentCounter = 0;
		var divs = dhtmlgoodies_tabObj[parentId].getElementsByTagName('DIV');
		for(var no=0;no<divs.length;no++){
			if(divs[no].className=='dhtmlgoodies_aTab'){
				divs[no].id = 'tabView' + parentId + '_' + tabTitleCounter;
				tabTitleCounter++;
			}
			if(divs[no].id.indexOf('tabTab')>=0){
				divs[no].id = 'tabTab' + parentId + '_' + tabContentCounter;
				tabContentCounter++;
			}
		}
		tabView_countTabs[parentId] = tabContentCounter;
	}

	//新建newTab
	function createNewTab(parentId,tabTitle,tabContent,tabContentUrl,closeButton){
		if(tabView_countTabs[parentId]>=tabView_maxNumberOfTabs)return;	// Maximum number of tabs reached - return
		var div = document.createElement('DIV');
		div.className = 'dhtmlgoodies_aTab';
		dhtmlgoodies_tabObj[parentId].appendChild(div);

		var tabId = initTabs(parentId,Array(tabTitle),0,'','',Array(closeButton),true);
		if(tabContent)div.innerHTML = tabContent;
		if(tabContentUrl){
			var ajaxIndex = ajaxObjects.length;
			ajaxObjects[ajaxIndex] = new sack();
			ajaxObjects[ajaxIndex].requestFile = tabContentUrl;	// Specifying which file to get

			ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId); };	// Specify function that will be executed after file has been found
			ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

		}
	}
	
	
	//_newTab(title,url,isButton)//新建newTab
	/*
	function _newTab(tabTitle,url,closeButton){
		delTab(tabTitle);
		var parentId= 'tabView_1';
		closeButton=true;
		if(tabView_countTabs[parentId]>=tabView_maxNumberOfTabs)return;// Maximum number of tabs reached - return
		var div = document.createElement('DIV');
		div.className = 'dhtmlgoodies_aTab';
		dhtmlgoodies_tabObj[parentId].appendChild(div);

		var tabId = initTabs(parentId,Array(tabTitle),0,'','',Array(closeButton),true);
		tabId= "1_"+tabId;
		url = url.J({tabId:tabId});
		//url +=(url.indexOf("?") != -1)?"&tabId="+tabId:"?tabId="+tabId;
		//ids="jes"+ids;
		var html='<iframe style="width:100%;height:470px;" id="jes_'+tabId+'" name="jes_'+tabId+'" frameborder="0" scrolling="auto"  src="'+url+'"></iframe>';
		div.innerHTML = html;
	}
	*/
	
	//_newTab(title,url,isButton)//新建newTab
	function _newTab(tabTitle,url,closeButton,height){
		delTab(tabTitle);
		var parentId= 'tabView_1';
		closeButton=true;
		if(tabView_countTabs[parentId]>=tabView_maxNumberOfTabs)return;// Maximum number of tabs reached - return
		var div = document.createElement('DIV');
		div.className = 'dhtmlgoodies_aTab';
		dhtmlgoodies_tabObj[parentId].appendChild(div);

		var tabId = initTabs(parentId,Array(tabTitle),0,'','',Array(closeButton),true);
		tabId= "1_"+tabId;
		url = url.J({tabId:tabId});
		//url +=(url.indexOf("?") != -1)?"&tabId="+tabId:"?tabId="+tabId;
		//ids="jes"+ids;
		var html='<iframe style="width:100%;" id="jes_'+tabId+'" height="473" name="jes_'+tabId+'" frameborder="0" scrolling="auto"></iframe>';
		div.innerHTML = html;
		Jes('jes_'+tabId).src='';
		Jes('jes_'+tabId).src=url;//ie6 bug
		//Jes('jes_'+tabId).style.height=window.screen.availHeight;
		if(height){
			Jes('jes_'+tabId).height=height+'px';
		}else{
			Jes._ifTimer[tabId] = window.setInterval("Jes.reIframe('jes_"+tabId+"','"+tabId+"')", 1000);
		}
		/*
var bookmark;
var range = document.selection.createRange();
bookmark = range.getBookmark();
var newRange = document.body.createTextRange();
newRange.moveToBookmark(bookmark);//将光标移到原来的选中区域
newRange.collapse();
newRange.select();
*/
	}

	function getTabIndexByTitle(tabTitle)
	{
		var regExp = new RegExp("(.*?)&nbsp.*$","gi");
		tabTitle = tabTitle.replace(regExp,'$1');
		//做修改 hyh
		for(var prop in dhtmlgoodies_tabObj){
			//alert(prop);
			var divs = dhtmlgoodies_tabObj[prop].getElementsByTagName('DIV');
			for(var no=0;no<divs.length;no++){
				if(divs[no].id.indexOf('tabTab')>=0){
					var span = divs[no].getElementsByTagName('SPAN')[0];
					var regExp2 = new RegExp("(.*?)&nbsp.*$","gi");
					var spanTitle = span.innerHTML.replace(regExp2,'$1');
					if(spanTitle == tabTitle){
						var tmpId = divs[no].id.split('_');
						return Array(prop,tmpId[tmpId.length-1].replace(/[^0-9]/g,'')/1);
					}
				}
			}
		}


		return -1;
	}

	/* Call this function if you want to display some content from external file in one of the tabs
	Arguments: Title of tab and relative path to external file */

	function addAjaxContentToTab(tabTitle,tabContentUrl){
		var index = getTabIndexByTitle(tabTitle);
		if(index!=-1){
			var ajaxIndex = ajaxObjects.length;

			tabId = index[1];
			parentId = index[0];


			ajaxObjects[ajaxIndex] = new sack();
			ajaxObjects[ajaxIndex].requestFile = tabContentUrl;	// Specifying which file to get

			ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId); };	// Specify function that will be executed after file has been found
			ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function

		}
	}


	//modify
	function delTab(tabLabel,tabIndex,parentId){
		if(tabLabel){
			var index = getTabIndexByTitle(tabLabel);
			if(index!=-1){
				delTab(false,index[1],index[0]);
			}
		}else if(tabIndex>=0){
			if(document.getElementById('tabTab' + parentId + '_' + tabIndex)){
				var obj = document.getElementById('tabTab' + parentId + '_' + tabIndex);
				var id = obj.parentNode.parentNode.id;
				//alert(tabIndex);
				//alert(document.getElementById('tabView' + parentId + '_' + tabIndex).innerHTML);
				obj.parentNode.removeChild(obj);
				var obj2 = document.getElementById('tabView' + parentId + '_' + tabIndex);
				obj2.innerHTML='';//obj2里面
				obj2.parentNode.removeChild(obj2);
				resetTabIds(parentId);
				activeTabIndex[parentId]=-1;
				showTab(parentId,tabIndex-1);//'0'为返回第一个
			}
		}
	}
	
	//hyh
	function _delTab(tabId){
		var _idx=tabId.split('_');
		delTab(false,_idx[1],"tabView_"+_idx[0]);//tabView_1
	}

/**
* @todo：创建Ajaxtab内容
* @date：2008-7-22 14:59
* @author：hyh
*/
/*
function createAjaxTabContent(parentId,tabId,url,func){
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	if(url){
		ajaxObjects[ajaxIndex].requestFile = url;
		ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId,func); };
		ajaxObjects[ajaxIndex].runAJAX();
	}
}
*/
function createAjaxTabContent(parentId,tabId,url,func){
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	if(url){
		ajaxObjects[ajaxIndex].requestFile = url;
		ajaxObjects[ajaxIndex].onCompletion = function(){ showAjaxTabContent(ajaxIndex,parentId,tabId,func); };
		ajaxObjects[ajaxIndex].runAJAX();
	}
}
function getRelativePath(){
	var url = location.href;
	var path = url.length - url.replace(/\//g,"").length - 4;
	var str = "";
	for(var i = 0; i < path; i++){
	   str += "../";
	}
	return str;
}